<?php
	/**
	 * 
	 * UserProduct model
	 * @author Tri
	 *
	 */
	class UserProductModel extends Model {
        /**
         * Constructor
         */
        function __construct()
        {
            parent::__construct();
        }
        
        /**
         * 
         * Check relation exist
         * @param integer $userId
         * @param integer $productId
         * @param integer $relationId
         */
        function checkRelationExist($userId, $productId, $relationId){
        	$sql = "select * from user_product where user_id = ".$userId." and product_id = ".$productId." and upr_id = ".$relationId;
            $res = $this->execute_query($sql);
            if(count($res) != 0){
                return true;
            }
            return false;
        }
        
        /**
         * 
         * Add relation product
         * @param integer $userId
         * @param integer $productId
         * @param integer $relationId
         */
        function addUserProductRelation($userId, $productId, $relationId){
        	if ($this->checkRelationExist($userId, $productId, $relationId))
        		return false;
        	else{
        		$sql = "insert into user_product (user_id, product_id, upr_id) values (".$userId.",".$productId.",".$relationId.")";
        		$res = $this->execute_command($sql);
        		if ($res)
        			return true;
        		else
        			return false;
        	}
        }
        
        /**
         * 
         * Remove relation product
         * @param integer $userId
         * @param integer $productId
         * @param integer $relationId
         */
		function removeUserProduct($userId, $productId, $relationId){
			if ($this->checkRelationExist($userId, $productId, $relationId)){
				$sql = "delete from user_product where user_id = ".$userId." and product_id = ".$productId." and upr_id = ".$relationId;
        		$res = $this->execute_command($sql);
        		if ($res)
        			return true;
        		else
        			return false;
			}
        	else{
        		return false;
        	}
        }
	}
?>